//
//  CLLConst.swift
//  DemoNavBar
//
//  Created by YangHuan on 2019/12/13.
//  Copyright © 2019 YangHuan. All rights reserved.
//


import UIKit
import DeviceKit

public let screenWidth = UIScreen.main.bounds.size.width

public let screenHeight = UIScreen.main.bounds.size.height

/// 状态栏高度
public let statusBarHeight: CGFloat = {
    if DeviceOption.currentDevice == .iphoneX {
        return 44.0
    }
    return 20.0
}()
/// 导航栏加状态栏高度
public let naviAddStatusHeight: CGFloat = {
    if DeviceOption.currentDevice == .iphoneX {
        return 88.0
    }
    return 64.0
}()

/// 导航栏高度
public let naviBarHeight: CGFloat = 44.0

/// tabbarHeight
public let tabbarHeight: CGFloat = {
    if DeviceOption.currentDevice == .iphoneX {
        return 83.0
    }
    return 49.0
}()

/// iphoneX底部空间高度
public let screenBottomHeight: CGFloat = {
    if DeviceOption.currentDevice == .iphoneX {
        return 34.0
    }
    return 0.0
}()

/// 获取 view 所在的控制器
public func currentController(view: UIView)-> UIViewController? {
    var next:UIView? = view
    repeat {
        if let nextResponder = next?.next,
            nextResponder.isKind(of: UIViewController.self) {
            return (nextResponder as! UIViewController)
        }
        next = next?.superview
    } while next != nil
    return nil
}
/// 获取 window 的根控制器
func getRootController() -> UIViewController? {
    var window = UIApplication.shared.keyWindow
    //是否为当前显示的window
    if window?.windowLevel != UIWindow.Level.normal{
        let windows = UIApplication.shared.windows
        for  windowTemp in windows{
            if windowTemp.windowLevel == UIWindow.Level.normal{
                window = windowTemp
                break
            }
        }
    }
    return window?.rootViewController
}
/// 获取当前所在 导航控制器
public func currentNavigationController() -> UINavigationController? {
    guard let rootVC = getRootController() else { return nil }
    if rootVC.isKind(of: UINavigationController.self) {
        return rootVC as? UINavigationController
    } else if rootVC.isKind(of: UITabBarController.self) {
        let vc = rootVC as! UITabBarController
        return vc.selectedViewController as? UINavigationController
    }
    return nil
}

/// 将颜色转换为图片

public func getImageWithColor(color:UIColor) -> UIImage{
    let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()
    context!.setFillColor(color.cgColor)
    context!.fill(rect)
    let image = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return image
}

